//
//  QZMyFansVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/5/17.
//

#import "QZMyFansVC.h"
#import "QZMyFansCell.h"
#import "QZFansUserModel.h"
#import "DCGoodParticularsViewController.h"

@interface QZMyFansVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *fansTableView;
@property (strong,nonatomic) PublicView * nullView;
@property (strong,nonatomic)NSMutableArray <QZFansUserModel *>* listArr;
@property (assign,nonatomic) CGFloat itemWdith;
@property (assign,nonatomic) NSInteger page;
@property (nonatomic, strong) id lastId;
@end

@implementation QZMyFansVC


-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(_listType == 0){
        self.navigationItem.title = NSLocalizedString(@"关注", nil);
    }else{
        self.navigationItem.title = NSLocalizedString(@"粉丝", nil);
    }
    self.view.backgroundColor = viewback_black;
    self.itemWdith = (KScreen_W-2)/2;
    
    KWeakSelf

    self.fansTableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf.fansTableView.mj_footer resetNoMoreData];
        [weakSelf.listArr removeAllObjects];
        [weakSelf.fansTableView reloadData];
        weakSelf.page = 0;
        [weakSelf getFansOrFollowsList];
    }];
    self.fansTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf getFansOrFollowsList];
    }];
    
    [SVProgressHUDTool showWithStatus:nil];
    [self getFansOrFollowsList];
}
#pragma mark 懒加载
- (NSMutableArray *)showTextViewToKey1:(NSString *)keyString{
    NSMutableArray *showTextViewArrTo1 = [NSMutableArray new];
    for (int i = 0; [showTextViewArrTo1 count] < 5; i++) {
        int  RandomNumber = arc4random() % 30;
        if(![showTextViewArrTo1 containsObject:[NSNumber numberWithInt:RandomNumber]]){
            [showTextViewArrTo1 addObject:[NSNumber numberWithInt:RandomNumber]];
        }
    }
    return showTextViewArrTo1;
}
- (NSInteger)showTextViewToTime1:(NSString *)time{
    NSDateFormatter *showTextViewFaTo1 = [[NSDateFormatter alloc] init];
    [showTextViewFaTo1 setDateFormat:@"yyyy-MM-dd"];
    return [[showTextViewFaTo1 dateFromString:time] timeIntervalSince1970];
}
- (NSMutableArray *)listArr{
    if(_listArr == nil){
        _listArr = [NSMutableArray new];
    }
    return _listArr;
}
- (UITableView *)fansTableView{
    if(_fansTableView == nil){
        _fansTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KNav_H)];
        _fansTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _fansTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _fansTableView.backgroundColor = [UIColor clearColor];
        _fansTableView.delegate = self;
        _fansTableView.dataSource = self;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_fansTableView];
    }
    return _fansTableView;
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-KNav_H-200)/2, KScreen_W, 200)];
        if(self.listType == 0){
            _nullView.publicLabel.text = @"暂无关注用户";
        }else{
            _nullView.publicLabel.text = @"暂无粉丝";
        }
        
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
#pragma mark 请求粉丝和关注
- (void)getFansOrFollowsList{
    KWeakSelf
    KMutableDic(sendDic)
    if (self.otheMid != nil) {
        [sendDic setValue:self.otheMid forKey:@"tomid"];
    }
    if (self.page != 0 && self.listType == 0) {
        [sendDic setObject:_lastId forKey:@"last_id"];
    }
    sendDic[@"page"] = @(self.page);
    
    if(_listType == 0){
        [YBNetBase userFollowListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
            if(code == 200){
                [weakSelf showListData:data];
            }else{
                [YBMsgPop showPop:msg];
            }
        }];
    }else{
        [YBNetBase userFansListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
            if(code == 200){
                [weakSelf showListData:data];
            }else{
                [YBMsgPop showPop:msg];
            }
        }];
    }
}
- (void)showListData:(NSDictionary *)data{
    if (data[@"last_id"] != nil && self.listType == 0){
        self.lastId = data[@"last_id"];
    }
    
    for (NSDictionary * dic in data[@"list"]) {
        QZFansUserModel * model = [[QZFansUserModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.listArr addObject:model];
    }
    [self.fansTableView reloadData];
    if([self.fansTableView.mj_header isRefreshing]){
        [self.fansTableView.mj_header endRefreshing];
    }else if ([self.fansTableView.mj_footer isRefreshing]){
        if([data[@"list"] count] == 0){
            [self.fansTableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.fansTableView.mj_footer endRefreshing];
        }
    }else{
        if(self.listArr.count == 0){
            self.nullView.hidden = NO;
            self.fansTableView.mj_footer.hidden = YES;
        }else{
            self.nullView.hidden = YES;
            self.fansTableView.mj_footer.hidden = NO;
        }
    }
    [SVProgressHUDTool dismiss];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QZMyFansCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QZMyFansCell"];
    if(cell == nil){
        cell = [[QZMyFansCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"QZMyFansCell"];
    }
    cell.fansModel = self.listArr[indexPath.row];
    KWeakSelf
    cell.fansListTapBlock = ^(NSInteger index) {
        if(index == 0){
            [weakSelf followsTap:indexPath];
        }
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [QZUserInfoModel otherMineControlView:self mineId:self.listArr[indexPath.row].mid videoId:nil];
}
- (void)followsTap:(NSIndexPath *)indexPath{
    KWeakSelf
    KMutableDic(sendDic)
    [sendDic setObject:self.listArr[indexPath.row].mid forKey:@"to_mid"];
    [YBNetBase userFollowUserRequest:sendDic target:self successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            if([weakSelf.listArr[indexPath.row].isfollow boolValue]){
                weakSelf.listArr[indexPath.row].isfollow = @(0);
                [YBMsgPop showPop:@"已取消关注"];
            }else{
                weakSelf.listArr[indexPath.row].isfollow = @(1);
                [YBMsgPop showPop:@"已关注"];
            }
            [weakSelf.fansTableView reloadData];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}

@end
